package com.ipet.ad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ipet.ad.mapper.AdMapper;
import com.ipet.ad.pojo.Ad;
import com.ipet.ad.service.AdService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdServiceImpl extends ServiceImpl<AdMapper , Ad> implements AdService {
    @Override
    public PageInfo<Ad> getAllAd(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo , pageSize);
        List<Ad> ads = this.baseMapper.selectList(null);
        PageInfo<Ad> pageInfo = new PageInfo<>(ads);
        return pageInfo;
    }

    @Override
    public PageInfo<Ad> getSomeAd(Map<String , Object> map , Integer pageNo, Integer pageSize) {
        QueryWrapper<Ad> adQueryWrapper = queryWrapper(map);
        PageHelper.startPage(pageNo , pageSize);
        List<Ad> ads = this.baseMapper.selectList(adQueryWrapper);
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        return adPageInfo;
    }
    private QueryWrapper<Ad> queryWrapper(Map<String , Object> map){
        QueryWrapper<Ad> wrapper = null;
        if(map != null){
            Object adName = map.get("adName");
            Object adStatus = map.get("adStatus");
            Object startTime = map.get("startTime");
            Object endTime = map.get("endTime");
            if(adName != null || adStatus != null || startTime != null || endTime != null){
                wrapper = new QueryWrapper<>();
                if(adName != null){
                    wrapper.like("ad_name" , adName);
                }
                if(adStatus != null){
                    wrapper.eq("ad_status" , adStatus);
                }
                if(startTime != null){
                    wrapper.ge("start_time" , startTime);
                }
                if(endTime != null){
                    wrapper.le("end_time" , endTime);
                }
                System.out.println(adName);
            }
        }
       return  wrapper;
    }
}
